package me.zhengjie.modules.rest.enterprise;

import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.domain.enterprise.EnterpriseInvestmentInstitutions;
import me.zhengjie.modules.service.EnterpriseInvInsService;
import me.zhengjie.modules.utils.Contents;
import me.zhengjie.modules.utils.JsonUtils;
import me.zhengjie.modules.utils.ReturnObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class EnterpriseInvInsController {

    private final EnterpriseInvInsService enterpriseInvInsService;

    @PostMapping("/EnterInvIns/addEnterpriseInvInsByList")
    @AnonymousAccess
    public @ResponseBody Object addEnterpriseInvInsByList(@RequestBody String json){
        List<EnterpriseInvestmentInstitutions> institutionsList= JsonUtils.JsonToEnterpriseInvIns(json);
        institutionsList.forEach(i->{
            i.setCompaniesId((long) 0);
        });
        ReturnObject returnObject=new ReturnObject();
        try {
            int num=enterpriseInvInsService.addEnterpriseInvInsByList(institutionsList);
            if(num>0){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contents.RETURN_CODE_FAIL);
                returnObject.setMessage(Contents.RETURN_MESSAGE_FAIL);
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contents.RETURN_CODE_FAIL);
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }

}
